package cn.exrick.xboot.modules.backlogOrFullList.controller;

import cn.exrick.xboot.common.constant.CommonConstant;
import cn.exrick.xboot.common.constant.SecurityConstant;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.backlogOrFullList.entity.APPBacklogOrFullList;
import cn.exrick.xboot.modules.backlogOrFullList.entity.BacklogOrFullList;
import cn.exrick.xboot.modules.backlogOrFullList.service.IbacklogOrFullListService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * @author Exrick
 */
@Slf4j
@RestController
@Api(description = "待办已办列表管理接口")
@RequestMapping("/xboot/backlogOrFullList")
@Transactional
public class backlogOrFullListController {

    @Autowired
    private IbacklogOrFullListService ibacklogOrFullListService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取")
    public Result<BacklogOrFullList> get(@PathVariable String id){

        BacklogOrFullList backlogOrFullList = ibacklogOrFullListService.getById(id);
        return new ResultUtil<BacklogOrFullList>().setData(backlogOrFullList);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取全部数据")
    public Result<List<BacklogOrFullList>> getAll(){

        List<BacklogOrFullList> list = ibacklogOrFullListService.list();
        return new ResultUtil<List<BacklogOrFullList>>().setData(list);
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "分页获取")
    public Result<IPage<BacklogOrFullList>> getByPage(@ModelAttribute PageVo page){

        IPage<BacklogOrFullList> data = ibacklogOrFullListService.page(PageUtil.initMpPage(page));
        return new ResultUtil<IPage<BacklogOrFullList>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据")
    public Result<BacklogOrFullList> saveOrUpdate(@ModelAttribute BacklogOrFullList backlogOrFullList){
        log.info("执行saveOrUpdate开始");
        backlogOrFullList.setUpdateTime(new Date());
        if(ibacklogOrFullListService.saveOrUpdate(backlogOrFullList)){
            return new ResultUtil<BacklogOrFullList>().setData(backlogOrFullList);
        }
        return new ResultUtil<BacklogOrFullList>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{ids}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除")
    public Result<Object> delAllByIds(@PathVariable String[] ids){

        for(String id : ids){
            ibacklogOrFullListService.removeById(id);
        }
        return new ResultUtil<Object>().setSuccessMsg("批量通过id删除数据成功");
    }
    @RequestMapping(value = "/selectBacklogOrFullListPageVoMap", method = RequestMethod.GET)
    @ApiOperation(value = "待办已办列表条件查询")
    public Result<IPage<BacklogOrFullList>> selectBacklogOrFullListPageVoMap(@ModelAttribute Page page,@ModelAttribute BacklogOrFullList backlogOrFullList) {
        log.info("调用接口selectBacklogOrFullListPageVoMapbegin开始"+new Date());
        log.info("传入参数backlogOrFullList："+backlogOrFullList.toString());
        IPage<BacklogOrFullList> backlogOrFullListIPage = ibacklogOrFullListService.selectBacklogOrFullListPageVoMap(page, backlogOrFullList);
        log.info("调用接口selectBacklogOrFullListPageVoMapbegin结束"+new Date());
        return new ResultUtil<IPage<BacklogOrFullList>>().setData(backlogOrFullListIPage);
    }



    @RequestMapping(value = "/addBacklogOrFullList", method = RequestMethod.POST)
    @ApiOperation(value = "添加待办已办")
    public Result<Object> addBacklogOrFullList(@ModelAttribute BacklogOrFullList backlogOrFullList) {
        log.info("调用接口addBacklogOrFullList开始"+new Date());
        log.info("传入参数backlogOrFullList："+backlogOrFullList.toString());
        backlogOrFullList.setCreateTime(new Date());
        backlogOrFullList.setUpdateTime(new Date());
        if(ibacklogOrFullListService.save(backlogOrFullList)){
            return new ResultUtil<Object>().setSuccessMsg("添加成功");
        }
        log.info("调用接口addBacklogOrFullList结束"+new Date());
        return new ResultUtil<Object>().setSuccessMsg("添加失败");
    }

    @RequestMapping(value = "/selectBacklogOrFullListApp", method = RequestMethod.GET)
    @ApiOperation(value = "查询APP的待办展示列表")
    public Result<List<APPBacklogOrFullList>> selectBacklogOrFullListApp(@ModelAttribute Page page, @ModelAttribute BacklogOrFullList backlogOrFullList, HttpServletRequest request) {
        //            //查询APP的待办展示列表

        String headerToken = request.getHeader(SecurityConstant.HEADER);
        String v = stringRedisTemplate.opsForValue().get(SecurityConstant.APP_TOKEN_PRE + headerToken);
        if (v == null || v == ""){
            return new ResultUtil<List<APPBacklogOrFullList>>().setErrorMsg(401,"登录已失效，请重新登录!");
        }
        List<APPBacklogOrFullList> appBacklogOrFullLists = ibacklogOrFullListService.selectBacklogOrFullListAPP(backlogOrFullList);
        log.info("查询APP的待办展示列表："+appBacklogOrFullLists.size());
        for (APPBacklogOrFullList a:appBacklogOrFullLists) {
            a.setInfoID(null);
        }
        return new ResultUtil<List<APPBacklogOrFullList>>().setData(appBacklogOrFullLists);
    }

    @RequestMapping(value = "/selectBacklogOrFullListApptest", method = RequestMethod.GET)
    @ApiOperation(value = "查询APP的待办展示列表")
    public Result<List<APPBacklogOrFullList>> selectBacklogOrFullListApptest(@ModelAttribute Page page, @ModelAttribute BacklogOrFullList backlogOrFullList, HttpServletRequest request) {
        //            //查询APP的待办展示列表

        List<APPBacklogOrFullList> appBacklogOrFullLists = ibacklogOrFullListService.selectBacklogOrFullListAPP(backlogOrFullList);
        log.info("查询APP的待办展示列表："+appBacklogOrFullLists.size());
        for (APPBacklogOrFullList a:appBacklogOrFullLists) {
            a.setInfoID(null);
        }
        return new ResultUtil<List<APPBacklogOrFullList>>().setData(appBacklogOrFullLists);
    }



    @RequestMapping(value = "/APP/backlogOrFulls", method = RequestMethod.GET)
    @ApiOperation(value = "APP待办列表条件查询")
    public Result<IPage<BacklogOrFullList>> selectBacklogOrFullListPageVoMapApp(@ModelAttribute Page page,@ModelAttribute BacklogOrFullList backlogOrFullList) {
        log.info("调用接口selectBacklogOrFullListPageVoMapAppbegin开始"+new Date());
        backlogOrFullList.setCode(CommonConstant.todo);//添加待办标识
        log.info("传入参数backlogOrFullList："+backlogOrFullList.toString());
        IPage<BacklogOrFullList> backlogOrFullListIPage = ibacklogOrFullListService.selectBacklogOrFullListPageVoMapApp(page, backlogOrFullList);
        log.info("调用接口selectBacklogOrFullListPageVoMapAppbegin结束"+new Date());
        return new ResultUtil<IPage<BacklogOrFullList>>().setData(backlogOrFullListIPage);
    }


    }
